今天先來將之前繼承mail的功能,附加到checkout model上面。
先到library_checkout/manifest.py,指定要的model
"depends": ["library_member", "mail"],
library_checkout/models/library_checkout.py
class Checkout(models.Model):
_inherit = ["mail.thread", "mail.activity.mixin"]
library_checkout/views/checkout_view.xml
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />
<field name="activity_ids" widget="mail_activity"/>
<field name="message_ids" widget="mail_thread" />
</div>
這樣之後checkout model就有基本的傳訊息功能。
接下來要做wizard model,向借書的人通知,例如可以選擇過期尚未還書的人,向他們發送一條訊息,請求歸還書籍。
library_checkout/init.py
from . import wizard
wizard/init.py
from . import checkout_mass_message
wizard/checkout_mass_message.py
class CheckoutMassMessage(models.TransientModel):
_name = "library.checkout.massmessage"
_description = "Send Message to Borrowers"
checkout_ids = fields.Many2many(
"library.checkout",
string="Checkouts",
)
message_subject = fields.Char()
message_body = fields.Html()
還是要設置安全性,security/ir.model.access.csv
checkout_massmessage_user,Checkout Mass Message User,model_library_checkout_massmessage,library_app.library_group_user,1,1,1,1
再來就是介面,wizard/checkout_mass_message_wizard_view.xml
<odoo>
<record id="view_form_checkout_message" model="ir.ui.view">
<field name="name">Library Checkout Mass Message Wizard</field>
<field name="model">library.checkout.massmessage</field>
<field name="arch" type="xml">
<form>
<group>
<field name="message_subject" />
<field name="message_body" />
<field name="checkout_ids" />
</group>
<footer>
<button type="object"
name="button_send"
string="Send Messages" />
<button special="cancel"
string="Cancel"
class="btn-secondary" />
</footer>
</form>
</field>
</record>
<record id="action_checkout_message"
model="ir.actions.act_window">
<field name="name">Send Messages</field>
<field name="res_model">library.checkout.massmessage</field>
<field name="view_mode">form</field>
<field name="binding_model_id"
ref="model_library_checkout" />
<field name="binding_view_types">form,list</field>
<field name="target">new</field>
</record>
</odoo>
也要記得到data添加library_checkout/manifest.py
"wizard/checkout_mass_message_wizard_view.xml",
到checkout裡選擇任意訂單,裡面Action會有一個選項Send Messages:
不過按鈕還沒有作用,到wizard/checkout_mass_message.py
def button_send(self):
self.ensure_one()
if not self.checkout_ids:
raise exceptions.UserError(
"No Checkouts were selected."
)
if not self.message_body:
raise exceptions.UserError(
"A message body is required"
)
for checkout in self.checkout_ids:
checkout.message_post(
body=self.message_body,
subject=self.message_subject,
subtype_xmlid='mail.mt_comment',
)
return True
這樣我們把checkout model的功能都寫完了,最後就來做unit tests:
測試的資料就不用附加到__init__.py,直接新增tests/init.py
from . import test_checkout_mass_message
再來也要有基本框架,tests/test_checkout_mass_message.py
from odoo import exceptions
from odoo.tests import common
class TestWizard(common.SingleTransactionCase):
def setUp(self, *args, **kwargs):
super(TestWizard, self).setUp(*args, **kwargs)
def test_01_button_send(self):
"""Send button should create messages on Checkouts"""
接下來更新要測試的model,並把--test-enable新增至odoo
(env15) $ odoo -c library.conf --test-enable -u library_checkout --stop-after-init
應該會找到類似的訊息
INFO library odoo.modules.module: odoo.addons.library_checkout.tests.test_checkout_mass_message running tests.
然後先設置一個測試資料,在tests/test_checkout_mass_message.py:
class TestWizard(common.SingleTransactionCase):
def setUp(self, *args, **kwargs):
super(TestWizard, self).setUp(*args, **kwargs)
# Setup test data
admin_user = self.env.ref('base.user_admin')
self.Checkout = self.env["library.checkout"]\
.with_user(admin_user)
self.Wizard = self.env['library.checkout.massmessage']\
.with_user(admin_user)
a_member = self.env['library.member']\
.create({'partner_id': admin_user.partner_id.id})
self.checkout0 = self.Checkout\
.create({'member_id': a_member.id})
Button_send()將一則訊息加入到訊息歷史記錄中,並測試訊息是否多一條
def test_01_button_send(self):
"""Send button should create messages on Checkouts"""
count_before = len(self.checkout0.message_ids)
Wizard0 = self.Wizard\
.with_context(active_ids=self.checkout0.ids)
wizard0 = Wizard0.create({
"message_subject": "Hello",
"message_body": "This is a message.",
})
wizard0.button_send()
count_after = len(self.checkout0.message_ids)
self.assertEqual(
count_before + 1,
count_after,
"Expected one additional message in the Checkout.",
)
這樣就能判斷程式是否正確。